<?php
class Core_Model_DbTable_Images extends YouNet_Db_Table
{
    const STATUS_DELETED = 0;    
    const STATUS_HIDDEN = -1;
    const STATUS_SHOWN  = 1;

    const TABLE_NAME = 'images';
    protected $_name = self::TABLE_NAME;
    protected $_rowClass = 'Core_Model_Image';
    static private $_instance;
        
    public static function getInstance()
    {
        if (!(self::$_instance instanceof Core_Model_DbTable_Images))
        {
            self::$_instance = new Core_Model_DbTable_Images();
        }
        return self::$_instance;
    }
    
    public function insertImage($values)
    {
        $data = array(
            'name'          => $values['name'],
            'file_name'     => $values['file_name'],
            //'description'   => $values['description'],
            'id_page'       => $values['id_page']
        );
        return $this->insert($data);
    }

    public function getImages()
    {
        $select = $this->select()
                ->setIntegrityCheck(false)
                ->where('status != ?',self::STATUS_DELETED);
        $result = $this->fetchAll($select);
        
        return $result;
    }
    public function getImageById($id)
    {
        $select = $this->select()
                ->setIntegrityCheck(false)
                ->where('id = ?',$id);        
        $result = $this->fetchRow($select);        
        if (!empty($result))
        {
            $result = $result->toArray();
        }
        return $result;
    }
    public function updateImageById($id, $values)
    {
        $data = array(
            'name'  => $values['name'],
            'description'   => $values['description'],
            'file_name' => $values['file_name']
        );
        $where = array(
            'id = ?' => $id
        );
        return $this->update($data,$where);
    }
    /**
     * Display at canvas page
     * @param type $id_page
     * @return type 
     */
    public function getSelectImages($id_page)
    {
        $select = $this->select()
                ->setIntegrityCheck(false)
                ->where('id_page = ?', $id_page)
                ->where('status != ?', self::STATUS_DELETED);
        return $select;
    }
    
    public function getSelectAvailableImages($id_page)
    {
        $select = $this->select()
                ->setIntegrityCheck(false)
                ->where('id_page = ?',$id_page)
                ->where('status  = ?',self::STATUS_SHOWN);
        return $select;
    }
    
 
    public function updateStatus($id, $status)
    {        
        $data = array(
            'status'    => $status
        );
        $where = array(
            'id = ?' => $id
        );        
        return $this->update($data, $where);
    }
}
?>